package util;

import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import exception.BusinessException;
import exception.HTTPClientException;

/**
 * <pre> 
 *  ZqElectron
 *  File: CommonSendRequestUtil.java
 *  ZqElectron, Inc.
 *  Copyright (C): 2015
 *  Description:
 *  发送请求工具类
 * 
 *  Notes:
 *  $Id: CommonSendRequestUtil.java 69862260-0 2014-12-24 23:59:59Z ZqElectron\TK $ 
 * 
 * @Revision History:
 * <<2015年2月3日下午5:22:01>>, <<Who>>, <<what>>  
 *  2015年2月3日		GaoBing		Initial.
 * </pre>
 */
public class CommonSendRequestUtil
{
	/**
	 * 发送POST请求.
	 *
	 * @param url
	 * @param params
	 * @param token
	 * @return 
	 */
	public static KKResponse sendRestPostRequest(String url, JSONObject params, String token)
	{
		KKResponse resp = new KKResponse();
		try
		{
			resp = KKHttpClient.urlPostMethod(url, params, token);
			//处理异常
			ExceptionUtil.handleBusiException(resp);
		}
		catch (HTTPClientException e)
		{
			throw new HTTPClientException(e.getCode(),e.getMessage());
		}
		catch(Exception e)
		{
			throw new BusinessException("未知错误,请联系管理员!");
		}
		
		return resp;
	}
	
	/**
	 * 发送POST请求(无token).
	 *
	 * @param url
	 * @param params
	 * @param token
	 * @return 
	 */
	public static KKResponse sendRestPostWithoutTokenRequest(String url, JSONObject params)
	{
		KKResponse resp = new KKResponse();
		try
		{
			resp = KKHttpClient.urlPostMethod4NoToken(url, params);
			//处理异常
			ExceptionUtil.handleBusiException(resp);
		}
		catch (HTTPClientException e)
		{
			throw new HTTPClientException(e.getCode(),e.getMessage());
		}
		catch(Exception e)
		{
			throw new BusinessException("未知错误,请联系管理员!");
		}
		
		return resp;
	}
	
	
	/**
	 * 发送DELETE请求.
	 *
	 * @param url
	 * @param params
	 * @param token
	 * @return 
	 */
	public static KKResponse sendRestDeleteRequest(String url, Map<String,String> params, String token)
	{
		KKResponse resp = new KKResponse();
		try
		{
			resp = KKHttpClient.urlDeleteMethod(url, params, token);
			//处理异常
			ExceptionUtil.handleBusiException(resp);
		}
		catch (HTTPClientException e)
		{
			throw new HTTPClientException(e.getCode(),e.getMessage());
		}
		catch(Exception e)
		{
			throw new BusinessException("未知错误,请联系管理员!");
		}
		
		return resp;
	}
	
	
	/**
	 * 发送GET请求.
	 *
	 * @param url
	 * @param params
	 * @param token
	 * @return 
	 */
	public static KKResponse sendRestGetRequest(String url, JSONObject params, String token)
	{
		KKResponse resp = new KKResponse();
		try
		{
			resp = KKHttpClient.urlGetMethod(url, params, token);
			//处理异常
			ExceptionUtil.handleBusiException(resp);
		}
		catch (HTTPClientException e)
		{
			throw new HTTPClientException(e.getCode(),e.getMessage());
		}
		catch(Exception e)
		{
			throw new BusinessException("未知错误,请联系管理员!");
		}
		
		return resp;
	}
	
	
	/**
	 * 发送PUT请求.
	 *
	 * @param url
	 * @param params
	 * @param token
	 * @return 
	 */
	public static KKResponse sendRestPutRequest(String url, JSONObject params,String token)
	{
		KKResponse resp = new KKResponse();
		try
		{
			resp = KKHttpClient.urlPutMethod(url, params, token);
			//处理异常
			ExceptionUtil.handleBusiException(resp);
		}
		catch (HTTPClientException e)
		{
			throw new HTTPClientException(e.getCode(),e.getMessage());
		}
		catch(Exception e)
		{
			throw new BusinessException("未知错误,请联系管理员!");
		}
		
		return resp;
	}
	
}

/*
*$Log: av-env.bat,v $
*/